package com.ctrip.ec.api.admin.controller.album;
import com.ctrip.ec.api.admin.model.album.AlbumImageModel;
import com.ctrip.ec.api.admin.model.album.AlbumModel;
import com.ctrip.ec.api.admin.model.album.AlbumTagModel;
import com.ctrip.ec.api.admin.model.query.QueryModel;
import com.ctrip.ec.biz.dao.album.AlbumImageDao;
import com.ctrip.ec.biz.dao.album.AlbumTagDao;
import com.ctrip.ec.biz.dao.goods.GoodsDao;
import com.ctrip.ec.biz.dao.image.ImageDao;
import com.ctrip.ec.biz.entity.album.AlbumImageEntity;
import com.ctrip.ec.biz.entity.album.AlbumTagEntity;
import com.ctrip.ec.biz.entity.goods.GoodsEntity;
import com.ctrip.ec.biz.entity.image.ImageEntity;
import com.ctrip.ec.dal.PagerSetting;
import com.ctrip.ec.api.admin.common.RequestContext;
import com.ctrip.ec.biz.dao.album.AlbumDao;
import com.ctrip.ec.biz.entity.album.AlbumEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("album")
public class AlbumController{
    @Autowired
    private AlbumDao album;

    @Value("${imageDomain}")
    private  String imageDomain;

//    @Autowired
//    private ImageDao image;

    @Autowired
    private AlbumImageDao albumImage;



    @Autowired
    private GoodsDao goods;

    @Autowired
    private AlbumTagDao albumTag;


    @PostMapping("list")
    public List<AlbumModel> list(@RequestBody QueryModel model) throws  SQLException{
        PagerSetting setting = model.build();
        List<AlbumEntity> list = album.list(setting);

        List<AlbumModel> models = new ArrayList<>();
        List<AlbumImageEntity> imageList = albumImage.listByIds(list.stream().map(t->t.getMasterImageId()).distinct().collect(Collectors.toList()));
        for(AlbumEntity entity :list){
            AlbumModel model2   = new AlbumModel(entity);
            Optional<AlbumImageEntity> imageEntity = imageList.stream().filter(t->t.getId().equals(entity.getMasterImageId())).findFirst();
            if(imageEntity.isPresent()) {
                String imageUrl = imageEntity.get().getImageUrl();
                model2.setMasterImageUrl(imageUrl);
            }
            models.add(model2);
        }

        RequestContext.setResponseHeaderCount(setting.getRecordCount());

        return models;



    }

    @GetMapping("detail/{id}")
    public AlbumModel detail(@PathVariable Long id) throws SQLException {
        AlbumModel model = new AlbumModel(album.entity(id));

        model.setMasterImageUrl(albumImage.entity(model.getMasterImageId()).getImageUrl());

        List<AlbumImageEntity> imageList = albumImage.list("album_id="  + model.getId());
        List<AlbumImageModel> imageModelList = new ArrayList<>();

        for(AlbumImageEntity imageEntity :imageList){
            AlbumImageModel imageModel = new AlbumImageModel(imageEntity);
            List<AlbumTagEntity> albumTagList = albumTag.list("album_image_id=" + imageModel.getId());
            List<AlbumTagModel> albumTagModelList = new ArrayList<>();
            for(AlbumTagEntity albumTagEntity :albumTagList){
                AlbumTagModel albumTagModel = new AlbumTagModel(albumTagEntity);
                GoodsEntity goodsEntity = goods.entity(albumTagEntity.getGoodsId());
                albumTagModel.setMinPrice(goodsEntity.getMinPrice());
                albumTagModel.setMaxPrice(goodsEntity.getMaxPrice());
                albumTagModelList.add(albumTagModel);
            }
            imageModel.setTagList(albumTagModelList);
            imageModelList.add(imageModel);
        }
        model.setImageList(imageModelList);
        return model;
    }

    @PostMapping("save")
    public  Long save(@RequestBody AlbumModel model) throws SQLException {
        album.save(model);

        if(model.getImageList()!= null && model.getImageList().size()>0) {
            for (AlbumImageModel imageModel : model.getImageList()) {
                imageModel.setAlbumId(model.getId());
                albumImage.save(imageModel);
                if(imageModel == model.getImageList().get(0)){
                    model.setMasterImageId(imageModel.getId());
                    album.save(model);
                }
                if(imageModel.getTagList()!= null && imageModel.getTagList().size()>0){
                    for(AlbumTagEntity tagEntity : imageModel.getTagList()){
                        tagEntity.setAlbumImageId(imageModel.getId());
                        albumTag.save(tagEntity);
                    }
                }
            }
        }

        return model.getId();
    }

    @PostMapping("delete")
    public  void delete(@RequestBody List<Long> ids) throws SQLException {
        albumTag.deleteByAlbumIds(ids);
        albumImage.deleteByAlbumIds(ids);
        album.delete(ids);
    }
    @PostMapping("erase")
    public void erase(@RequestBody List<Long> ids) throws  SQLException{
        album.erase(ids);
    }

    @PostMapping("unerase")
    public  void unerase(@RequestBody List<Long> ids) throws  SQLException{
        album.unerase(ids);

    }
}
